package com.drojian;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class MainActivity extends AppCompatActivity {
    private Retrofit retrofit;
    private HttpBinService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

    }

    public void Sync(View view) {
        retrofit = new Retrofit.Builder().baseUrl("https://www.httpbin.org/").build();
        service = retrofit.create(HttpBinService.class);
        Call<ResponseBody> call = service.post("张三", "123");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response != null && response.isSuccessful()) { // 检查response是否成功
                    ResponseBody body = response.body();
                    if (body != null) {
                        try {
                            String result = body.string();
                            Log.d("xxx", result);
                        } catch (IOException e) {
                            Log.e("xxx", "IOException while trying to read response body", e);
                        }
                    } else {
                        Log.e("xxx", "ResponseBody is null");
                    }
                } else {
                    Log.e("xxx", "Response is not successful");
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}